找传奇、传世资源到传世资源站!

android_点餐app源码(含服务端以及数据库脚本)_

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

package food.test.zqz;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import food.test.R;
import food.test.domain.Food;
import food.test.util.DButil;
import food.test.wjf.ShopCart;
import food.test.wjf.ShopCartShowAll;
import food.test.zqz.util.GetNetWorkData;

public class ActivityShowFoodByType extends Activity {

	// 用于存储需要显示的数据
	private ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

	// 用于接收服务端
	private ArrayList<Food> serverFoods = new ArrayList<Food>();

	private GridView gv = null;

	public final static int SHOW_FOOD_DETAIL = 0;

	public final static int CONFIRM_PURCHASE = 1;
	
	public final static int LOOK_UP_SHOPPING = 2;
	
	private DButil db;

	// 用户在界面上选中的所有商品信息
	public Food selectedFood = new Food();

	/**
	 * 根据菜品类型,获取服务器端的菜品信息
	 * 
	 * @return
	 */
	private ArrayList<Food> getServerFoodInfoByType(int flagType) {

		serverFoods.clear();
		
		HashMap<String, String> request = new HashMap<String, String>();
		// 获得服务端数据"根据约定的字符串格式"
		String resData = GetNetWorkData.getConnectionPost(
				GetNetWorkData.URL_AllFood, request).toString();
		// String resData =
		// "[foodName=test;foodType=0;foodPrice=33.3f;foodImage=500008.jpg],[foodPrice=44;foodType=0;foodName=红烧肉;foodImage=500022.jpg]";
		ArrayList<Food> serverAllFoods = GetNetWorkData
				.decodeResponseData(resData);
		Food oneFood;
		Iterator<Food> it = serverAllFoods.iterator();
		while (it.hasNext()) {

			oneFood = it.next();
			if (oneFood.foodType == flagType) {

				serverFoods.add(oneFood);
			}
		}

		return serverFoods;
	}// end of getServerData()

	/**
	 * 准备显示在界面的数据
	 */
	private void prepareView() {

		data.clear();
		
		// 获取要显示菜品的类型
		Intent intent = this.getIntent();
		int showType = intent.getFlags();

		// 获取指定类型的菜品
		ArrayList<Food> assignedFoods = getServerFoodInfoByType(showType);

		// 设定界面显示数据
		HashMap<String, Object> item;
		for (Food food : assignedFoods) {
			item = new HashMap<String, Object>();
			item.put("price", "价格:"  food.foodPrice);
			item.put("name", "菜名:"  food.foodName);
			item.put("icon", food.foodImage);
			data.add(item);
		}
	}// end of prepareView()

	public void onCreate(Bundle saved) {

		super.onCreate(saved);
		setContentView(R.layout.show);
		try {
			gv = (GridView) this.findViewById(R.id.grid);

			prepareView();

			MyGridViewAdapter mgva = new MyGridViewAdapter(this);

			gv.setAdapter(mgva);

			// 监听GridView点击事件
			gv.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					selectedFood = serverFoods.get(position);
					setTitle("测试选中事件---"  selectedFood.foodId);
					Log.e("----test selected---", ""  selectedFood.foodId);
					ActivityShowFoodByType.this.openOptionsMenu();
					Log.e("test GridView onClickListener", "------");
				}
			});
		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	
	protected void onRestart() {
		super.onRestart();
		Log.e("TabIntent------", "test--onRestart()" getIntent().getFlags());
	}

	protected void onResume() {
		super.onResume();
		prepareView();
		Log.e("TabIntent------", "test--onResume()" getIntent().getFlags());
	}

	protected void onStart() {
		super.onStart();
		Log.e("TabIntent------", "test--onStart()" getIntent().getFlags());
	}

	protected void onStop() {
		super.onStop();
		Log.e("TabIntent------", "test--onStop()" getIntent().getFlags());
	}

	protected void onPause() {
		super.onPause();
		Log.e("TabIntent------", "test--onPause()" getIntent().getFlags());
	}

	/**
	 * 添加功能Menu键
	 */
	public boolean onCreateOptionsMenu(Menu menu) {

		super.onCreateOptionsMenu(menu);
		menu.add(1, SHOW_FOOD_DETAIL, Menu.CATEGORY_SYSTEM, "菜品详情").setIcon(
				R.drawable.zqz_menu_lookdetail);
		menu.add(1, CONFIRM_PURCHASE, Menu.CATEGORY_SYSTEM, "加入购物车").setIcon(
				R.drawable.zqz_menu_purchase);
		return true;
	}

	/**
	 * 监听用户选中事件
	 */
	public boolean onOptionsItemSelected(MenuItem item) {

		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case SHOW_FOOD_DETAIL:
			// 跳转到详情显示页面
			Intent intent = new Intent(ActivityShowFoodByType.this, FoodDetail.class);
			intent.putExtra("foodId", selectedFood.foodId);
			intent.putExtra("foodName", selectedFood.foodName);
			intent.putExtra("foodDescri", selectedFood.foodDescri);
			intent.putExtra("foodType", selectedFood.foodType);
			intent.putExtra("foodPrice", selectedFood.foodPrice);
			intent.putExtra("foodImage", selectedFood.foodImage);
			startActivity(intent);
			break;
		case CONFIRM_PURCHASE:
			Log.e("test==", ""  CONFIRM_PURCHASE);
			if (selectedFood.foodId != 0) {
				// 跳转到购物车界面,并且提示用户输入要几份菜品
				final View zqz_dialog = LayoutInflater.from(this).inflate(
						R.layout.zqz_dialog, null);

				// 提示用户输入数量
				new AlertDialog.Builder(this).setTitle("选购").setIcon(
						R.drawable.tap_vegetarian2).setView(zqz_dialog)
						.setPositiveButton("订购", new OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {

								EditText etnumber = (EditText) zqz_dialog
										.findViewById(R.id.zd_etnumber);
								String etnumberStr = etnumber.getText()
										.toString().trim();
								if (!etnumberStr.equals("")
										&& etnumberStr != null) {
									db = new DButil(ActivityShowFoodByType.this);
									ShopCart shopcart = new ShopCart();
									shopcart.setFoodId(selectedFood.foodId);
									shopcart.setFoodName(selectedFood.foodName);
									shopcart
											.setFoodPrice(selectedFood.foodPrice);
									int foodnum = Integer.parseInt(etnumberStr);
									shopcart.setFoodNum(foodnum);
									// float sumprices = selectedFood.foodPrice
									// * foodnum;
									float sumprices = getTotalMoney(
											selectedFood.foodPrice, foodnum);
									shopcart.setSumPrices(sumprices);
									db.addFood(shopcart);
									db.close();
									Log.e("-------- shopcart", ""  shopcart);
									Intent intent = new Intent(
											ActivityShowFoodByType.this,
											ShopCartShowAll.class);
									Bundle extras = new Bundle();
									extras
											.putInt("foodId",
													selectedFood.foodId);
									extras.putFloat("foodPrice",
											selectedFood.foodPrice);
									extras.putInt("foodNumber", Integer
											.parseInt(etnumberStr));
									extras.putString("foodName",
											selectedFood.foodName);
									extras.putString("用户名", "");
									extras.putInt("用户Id", 0);
									intent.putExtras(extras);
									startActivity(intent);
								} else {
									Toast.makeText(ActivityShowFoodByType.this,
											"请输入数量", Toast.LENGTH_SHORT).show();
								}
							}
						}).setNegativeButton("取消", new OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {

							}
						}).create().show();
			} else {

				Toast.makeText(ActivityShowFoodByType.this, "请选中菜品",
						Toast.LENGTH_LONG).show();
			}
			break;
		}// end of switch
		return true;
	}// end of onOptionsItemSelected()
	
	/**
	 * 计算(可能含小数位)总金额
	 */
	private float getTotalMoney(float price, int number) {

		BigDecimal bd_price = new BigDecimal(price);
		BigDecimal bd_number = new BigDecimal(number);

		return bd_price.multiply(bd_number).floatValue();
	}// end of getTotalMoney()
	
	/**
	 * Bean对象,用来封装GridView中的一个数据
	 * 
	 * @author zhuqiuzhu
	 * 
	 */
	public final class ViewHolder {
		public ImageView foodImagView;
		public TextView foodNameView;
		public TextView foodPriceView;
	}

	class MyGridViewAdapter extends BaseAdapter {

		LayoutInflater mInflater = null;

		public MyGridViewAdapter(Context ctx) {

			mInflater = LayoutInflater.from(ctx);
		}

		// 返回需要显示的数据集合的size
		public int getCount() {

			return data.size();
		}

		// 返回指定集合位置的子项数据
		public Object getItem(int position) {

			return data.get(position);
		}

		// 返回指定集合子项的ID号
		public long getItemId(int position) {

			return position;
		}

		// 返回某一条数据的视图
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder = null;

			// 若GridView 传进来的代表上一行数据的视图参数arg1为空,代表当前是第一行数据
			// 这个时候则解析代表一行数据的视图展现的xml文件zqz_gridview.xml
			// 并且把视图里的控件和bean对象里的控件关联起来
			if (convertView == null) {

				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.zqz_gridview, null);
				holder.foodImagView = (ImageView) convertView
						.findViewById(R.id.zg_gridicon);
				holder.foodNameView = (TextView) convertView
						.findViewById(R.id.zg_gridname);
				holder.foodPriceView = (TextView) convertView
						.findViewById(R.id.zg_gridprice);
				// 设置标识
				convertView.setTag(holder);
			} else {
				// 如果传进来的arg1不为空,则我们可以沿用上一行数据的视图,
				holder = (ViewHolder) convertView.getTag();
			}
			// 只是需要将数据替换成本行的数据即可

			/*-------------图片的显示实现--------------*/
			String fi = ((HashMap<String, Object>) data.get(position)).get(
					"icon").toString();
			String mUrl = GetNetWorkData.getAssignedFoodBitmapUrl(
					GetNetWorkData.URL, fi);
			GetNetWorkData.setAssignedFoodBimapView(mUrl, holder.foodImagView);
			/*----------------------------------------*/

			holder.foodNameView.setText(((HashMap<String, Object>) data
					.get(position)).get("name").toString());
			holder.foodPriceView.setText(((HashMap<String, Object>) data
					.get(position)).get("price").toString());
			return convertView;
		}

	}// end of MyGridViewAdapter

}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复